﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using ParaEditor.Desktop.Service.Entity;
using System.Data.OracleClient;
using ParaEditor.Infrastructure;

namespace ParaEditor.Desktop.Service.ModuleMgrService.Impl
{
    [Export(typeof(IModuleMgrService))]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public class ModuleMgrServiceImpl : IModuleMgrService
    {
        public bool CreateModule(MODULE _module)
        {
            bool flag = false;
            StringBuilder sb = new StringBuilder("insert into MODULE(MODULE_ID,MODULE_NAME,MODULE_VIEW,MODULE_SYS) values (");
            try
            {
                sb.Append(_module.MODULE_ID + ",");
                sb.Append("'" + _module.MODULE_NAME + "',");
                sb.Append("'" + _module.MODULE_VIEW + "',");
                sb.Append("'" + _module.MODULE_SYS + "'");
                sb.Append(")");
                DataBase.SqlCommand(sb.ToString());
                sb.Clear();
                flag = true;
            }
            catch (Exception ex)
            {
                flag = false;
            }
            return flag;
        }

        public bool IsExistByID(int id)
        {
            int count = -1;
            bool flag = false;
            OracleDataReader dataReader;
            string sqlText = "select count(*) from module where module_id = '" + id + "'";
            dataReader = DataBase.SqlReader(sqlText);
            while (dataReader.Read())
            {
                count = (int)dataReader.GetOracleNumber(0);
            }
            if (count == 0)
            {
                flag = false;
            }
            else
            {
                flag = true;
            }
            dataReader.Close();
            dataReader.Dispose();
            return flag;
        }

        public Modules GetModuleAll()
        {
            Modules modules = new Modules();
            OracleDataReader dataReader;
            try
            {
                string sqlText = "select  MODULE_SYS,MODULE_NAME,MODULE_ID,MODULE_VIEW from MODULE";
                dataReader = DataBase.SqlReader(sqlText);
                while (dataReader.Read())
                {
                    MODULE _module = new MODULE();
                    _module.MODULE_SYS = dataReader.GetOracleString(0).ToString();
                    _module.MODULE_NAME = dataReader.GetOracleString(1).ToString();
                    _module.MODULE_ID = (int)dataReader.GetOracleNumber(2);
                    _module.MODULE_VIEW = dataReader.GetOracleString(3).ToString();
                    modules.Add(_module);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            catch (Exception ex)
            {
            }
            return modules;
        }

        public Modules LoadModuleByOperator(OPERATOR optr)
        {
            OracleDataReader dataReader;
            Modules modules = new Modules();
            string moduleIDs = string.Empty;
            //获取optr的角色 再根据角色查询出module
            try
            {
                string sqlText1 = "select module_id";
                sqlText1 += " from";
                sqlText1 += "(select rule_id from operator where operator_id = '" + optr.OPERATOR_ID + "') t1,rule t2 where t1.rule_id = t2.rule_id";
                dataReader = DataBase.SqlReader(sqlText1);
                while (dataReader.Read())
                {
                    moduleIDs = dataReader.GetOracleString(0).ToString();
                }
                string sqlText2 = "select MODULE_ID,MODULE_NAME,MODULE_VIEW,MODULE_SYS from MODULE where MODULE_ID IN (" + moduleIDs + ")";
                dataReader = DataBase.SqlReader(sqlText2);
                while (dataReader.Read())
                {
                    MODULE _module = new MODULE();
                    _module.MODULE_ID = (int)dataReader.GetOracleNumber(0);
                    _module.MODULE_NAME = dataReader.GetOracleString(1).ToString();
                    _module.MODULE_VIEW = dataReader.GetOracleString(2).ToString();
                    _module.MODULE_SYS = dataReader.GetOracleString(3).ToString();
                    modules.Add(_module);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            catch (Exception ex)
            {
            }

            return modules;
        }
    }
}
